package com.somewater.display
{
	
	import com.somewater.control.IClear;
	import com.somewater.storage.Lib;
	import com.somewater.text.EmbededTextField;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.filters.DropShadowFilter;
	
	
	
	public class WindowButton extends Sprite implements IClear
	{
		private var groundUp:Sprite;
		private var groundOver:Sprite;
		private var textField:EmbededTextField;
		
		private var iconSprite:Sprite;
		
		private var _width:int = 100;
		private var _height:int = 48;
		
		public function WindowButton(label:String = null)
		{
			groundUp = Lib.getMc("ButtonGround");
			addChild(groundUp);
			groundOver = Lib.getMc("ButtonGround");
			groundOver.filters = [new DropShadowFilter(2, 45, 0x3D2816, 0.5, 6, 6)];
			addChild(groundOver);
			groundOver.visible = false;
			
			textField = new EmbededTextField(null, "b", 26, true);
			textField.mouseEnabled = false;
			addChild(textField);
			
			useHandCursor = buttonMode = true;
			
			addEventListener(MouseEvent.ROLL_OVER, onOver);
			addEventListener(MouseEvent.ROLL_OUT, onOut);
			addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			addEventListener(MouseEvent.MOUSE_UP, onUp);
			
			if(label)
				this.label = label;
		}
		
		public function clear():void
		{
			removeEventListener(MouseEvent.ROLL_OVER, onOver);
			removeEventListener(MouseEvent.ROLL_OUT, onOut);
			removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
			removeEventListener(MouseEvent.MOUSE_UP, onUp);
		}
		
		
		private function onOver(e:MouseEvent):void
		{
			//textField.scaleX = textField.scaleY = 1.1;
			groundOver.visible = true;
			groundUp.visible = false;
			resize();
		}
		
		private function onOut(e:MouseEvent):void
		{
			textField.scaleX = textField.scaleY = 1;
			groundOver.visible = false;
			groundUp.visible = true;
			resize();
		}
		
		private function onDown(e:MouseEvent):void
		{
			groundOver.visible = true;
			groundUp.visible = false;
		}
		
		private function onUp(e:MouseEvent):void
		{
			groundOver.visible = true;
			groundUp.visible = false;
		}
		
		public function setSize(w:int, h:int):void
		{
			_width = w;
			_height = h;
			
			resize();
		}
		
		private function resize():void
		{
			groundUp.width = _width;
			groundUp.height = _height;
			groundOver.width = _width;
			groundOver.height = _height;
			
			textField.x = (_width - textField.width - (iconSprite?iconSprite.width + 10:0)) * 0.5 + (iconSprite?iconSprite.width + 10:0);
			textField.y = (_height - textField.height) * 0.5;
			
			if(iconSprite)
			{
				iconSprite.x = 5;
				iconSprite.y = -iconSprite.height + 35;
			}
		}
		
		public function set label(text:String):void
		{
			textField.text = text;
			setSize(Math.max(_width, textField.width + 20 + (iconSprite?iconSprite.width + 10:0)), _height);
		}
		
		public function get label():String
		{
			return textField.text;
		}
		
		public function set icon(value:Sprite):void
		{
			if(iconSprite)
				removeChild(iconSprite);
			if(value)
			{
				iconSprite = value;
				addChild(iconSprite);
				iconSprite.mouseEnabled = false;
				setSize(Math.max(_width, textField.width + 20 + (iconSprite?iconSprite.width + 10:0)), _height);
			}
		}
		
		override public function get width():Number
		{
			return _width;
		}
		
		override public function get height():Number
		{
			return _height;
		}
	}
}